package genetic;

import genetic.interfaces.Individual;
import genetic.interfaces.Operator;
import genetic.interfaces.Population;
import genetic.interfaces.Selection;
import genetic.interfaces.StopCondition;

public class GeneticAlgorithm<I extends Individual<?, ?>>{
	private Population<I> pop;
	private Operator<I>   op;
	private Selection<I>  sel;
	private StopCondition stop;
	
	public GeneticAlgorithm(Population<I> pop,
							Selection<I> sel,
							Operator<I> op,
			                StopCondition stop){
		this.pop  = pop;
		this.op   = op;
		this.stop = stop;
		this.sel  = sel;
	}
	
	public I runGenetic(){
		pop.init();
		while(!stop.stopCondition()){
			I parents[] = sel.select();
			I child     = op.operation(parents[0], parents[1]);
			pop.insertIndividual(child);
		}
		return pop.getBest();
	}
}
